package com.vincent.baseapp.util;

import android.util.TypedValue;

import com.vincent.baseapp.BaseApplication;


/**
 * Created by Administrator on 2015/5/23.
 */
public class DensityUtils {
    private DensityUtils() {

    }

    /**
     * dp转px
     */
    public static int dp2px(int dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, BaseApplication.getContext().getResources().getDisplayMetrics());

    }

    /**
     * sp转px
     */
    public static int sp2px(float spValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, BaseApplication.getContext().getResources().getDisplayMetrics());
    }

    /**
     * px转成dip
     */
    public static int px2dp(int pxValue) {

        float density = BaseApplication.getContext().getResources().getDisplayMetrics().density;
        return (int) (pxValue / density + 0.5f);
    }

    /**
     * px转sp
     */
    public static int px2sp(int pxValue) {
        float scale = BaseApplication.getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / scale + 0.5f);
    }
}
